/******************************************************************************
* This file is part of is32p218 platform,
* Copyright (c) 2009 HangZhou InfoStrong CO.,LTD. All rights reserved.
* This software may only be used under the terms of a valid, current,
* end user license from InfoStrong.
* Nothing else gives you the right to use this software.   
*
* Name:   plc_hl_bind.h
* Desc:   PLC HL bind routine header file
* Author: Jerry
* Date:   2011-05-20
* Note:   
* History:
*
******************************************************************************/
#include "def.h"
#include "isOS.h"
#include "sys_glb.h"
#include "plc.h"
#include "afn_def.h"
#include "plc_if.h"

#ifndef __PLC_HL_BIND_H__
#define __PLC_HL_BIND_H__

#ifdef __cplusplus
extern "C" {
#endif

//------ MAC bind

#define PLC_BIND_KEEP_FLG			0			// DC's BIND_KEEP feature
#define PLC_BIND_IGNORE_FLG			0			// Slave's DC-Ignore-list feature
#define PLC_BIND_T2_TMOUT_FLG		0			// Master T2 timeout flg


#ifdef MASTER_NODE	// master-node
#define PLC_DC_MAC_BIND_S_INFO_BEST_FLG			0				// Record best s_info record flag
#define PLC_DC_MAC_BIND_S_INFO_MONI_FLG			0				// Monitor best s_info record flag

#define PLC_BIND_REQ_NODE_NUM_MAX	24		//Bind Request node number max
#define PLC_BIND_S_INFO_CFM_MAX		4
#define PLC_BIND_S_INFO_CSMA_MAX	2
#else	// Slave-node
#define PLC_BIND_IGNORE_LIST_MAX	4
#define PLC_BIND_RSP_DC_MAX			12		// Response DC MAX
#define PLC_BIND_REQUEST_MAX		100		// Bind request max number
#define PLC_BIND_DC_NUM_MAX			100
#endif

#define PLC_BIND_SELECT_THRESHOLD	4
#define PLC_BIND_DC_TRIGGER_NUM		4
typedef struct
{
	U8 m_mac[PLC_PH_MAC_LEN];
	U8 m_rssi;					// Format: 5+3(exp2)
	PLC_IF_BC_NUM_T m_bc_num;
#if defined(MASTER_NODE)&&PLC_DC_MAC_BIND_S_INFO_MONI_FLG
	U8 m_bc_num_moni;
	U8 m_rssi_moni;			// Format: 5+3(exp2)
#endif
}PLC_HL_BIND_NODE_INFO_T;

typedef PLC_HL_BIND_NODE_INFO_T PLC_HL_BIND_DC_RSP_INFO_T;

#ifdef MASTER_NODE
typedef struct
{
	PLC_HL_BIND_NODE_INFO_T m_req_info;		// Bind infomation of request node
#define PLC_BIND_S_INFO_BE_DEL		0xFFFF
	U16 m_resend_cnt;						// S-info resend cnt;
#if PLC_DC_MAC_BIND_S_INFO_BEST_FLG
	U16 m_life_cnt;							// S_info life count
#endif
	U8 m_snd_cnt;
}PLC_HL_BIND_REQ_NODE_INFO_T;
#endif

typedef struct
{
	U8 m_mac[PLC_PH_MAC_LEN];	// DC mac address
	U8 m_bind_cnt;				// Bind counter

	U8 m_rssi;					// Best infomation. Format: 5+3(exp2)
	U8 m_bc_num;				// 
}PLC_HL_BIND_DC_RECORD_T;

#ifdef PLC_DC_MAC_FIX
enum PLC_DC_MAC_FIX_MODE
{
	PLC_DC_MAC_FIX_MODE_NONE,
	PLC_DC_MAC_FIX_MODE_FIX,
};
#define PLC_DC_MAC_FIX_NUM_MAX	3
#endif

typedef struct
{
	U8 m_dc_mac_cnt;			// Clear command count
#if (defined(MASTER_NODE)&&(defined(PLC_VOID_COLLUDE)||defined(PLC_MAC_BIND)))
#ifdef PLC_MAC_BIND
	U8 m_bind_bc_num;			// Bind BC-Num
	U8 m_req_info_num;				// Request slave node number
	PLC_HL_BIND_REQ_NODE_INFO_T m_req_info[PLC_BIND_REQ_NODE_NUM_MAX];		// Bind infomation of request node
#endif
#endif

#if (!defined(MASTER_NODE))&&defined(PLC_MAC_BIND)
	U16 m_bind_timer1;					// PLC_DL_PARA_SET_CTRL_BIND_KEEP's duration
	BOOL m_mac_flg;						// MAC replace with Chip-SN
	U16 m_slot_val;						// Slot value of slave-node sending request message
	U8 m_req_always_flg;				// Bind request always flag
	U8 m_req_cnt;						// BIND_REQUEST counter
	U8 m_relay_best;					// History best relay number
	U8 m_rssi_best;						// History best rssi
	PLC_HL_BIND_NODE_INFO_T m_local_info;		// Bind infomation of local node
	PLC_HL_BIND_NODE_INFO_T m_moni_info;		// Bind infomation of monitor node
	PLC_HL_BIND_DC_RSP_INFO_T m_rsp_dc_info[PLC_BIND_RSP_DC_MAX];	// DC S-Info response buffer
	U8 m_rsp_dc_num;							// 
												
#if PLC_BIND_IGNORE_FLG
	U8 m_ignore_dc_mac_list[PLC_BIND_IGNORE_LIST_MAX][PLC_PH_MAC_LEN];
	U8 m_ignore_dc_num;
#endif

#ifdef PLC_BIND_DC_MANY
	PLC_HL_BIND_DC_RECORD_T m_bound_dc[PLC_BIND_RSP_DC_MAX];
	U8 m_bound_dc_num;		
	U8 m_bound_dc_cnt;
	U8 m_bind_trigger;			// Bind Trigger number
	U8 m_bind_threshold;		// Optimise bind threshold number
	U8 m_follow_flg;			// Follow bind flag
#endif

	isOS_TMR m_idle_mon_tmr;			// Idle Mon Timer
#ifndef MASTER_NODE
	U8 m_mac_backup[PLC_PH_MAC_LEN];	// Use to backup MAC in bind procedure
	#ifdef PLC_DC_MAC_FIX
	U8 m_bind_fix_cnt;
	U8 m_dc_mac2[PLC_PH_MAC_LEN];		// DC MAC in NVRAM (Pointed DC MAC )
	#endif
#endif
#endif
}PLC_HL_BIND_CTLR_T;

BOOL plc_hl_bind_init( void );
BOOL plc_hl_bind_dl_proc( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, BOOL *p_msg_rel_flg );
BOOL plc_hl_bind_dl_moni_proc( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, \
								BOOL *p_msg_rel_flg );

BOOL plc_hl_bind_hl_start( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, \
							 BOOL *p_msg_rel_flg );

BOOL plc_hl_bind_hl_clr_mac( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, \
							 BOOL *p_msg_rel_flg );

#ifdef MASTER_NODE


BOOL plc_hl_bind_hl_t1_tmout( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, \
							 BOOL *p_msg_rel_flg );
BOOL plc_hl_bind_hl_bind_cmd( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, \
							 BOOL *p_msg_rel_flg );
BOOL plc_hl_bind_hl_t2_tmout( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, \
							 BOOL *p_msg_rel_flg );
BOOL plc_hl_bind_hl_s_info_cfm_tmr_chk( void *p_arg );
BOOL plc_hl_bind_hl_s_info_cfm_tmout( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, \
							 BOOL *p_msg_rel_flg );
BOOL plc_hl_bind_hl_bind_end( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, \
							 BOOL *p_msg_rel_flg );
#else
#ifdef PLC_DC_MAC_FIX
void plc_hl_bind_dc_mac2_set( U8 *p_dc_mac );
#endif
BOOL plc_hl_bind_hl_start_tmout( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, \
							 BOOL *p_msg_rel_flg );
BOOL plc_hl_bind_hl_t1_tmout( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, \
							 BOOL *p_msg_rel_flg );
BOOL plc_hl_bind_hl_slot_tmout( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, \
							 BOOL *p_msg_rel_flg );
BOOL plc_hl_bind_hl_req_snd( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, \
							 BOOL *p_msg_rel_flg );
BOOL plc_hl_bind_hl_s_info_rsp_tmout( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, \
							 BOOL *p_msg_rel_flg );
void plc_hl_bind_ddt_flg_set( BOOL flg );
#endif

BOOL plc_hl_bind_app_ena( U8 *p_msg_data, U16 len, MSG_PLC_T msg_type, BOOL *p_msg_rel_flg );

#ifdef __cplusplus
}
#endif

#endif // end __PLC_HL_BIND_H__




